home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / t100.zoo / cookie.c < prev    next >
C/C++ Source or Header  |  1991-09-21  |  3KB  |  141 lines

  1. /*
  2.  *    cookie.c
  3.  */
  4.  
  5. #ifndef lint
  6. static char *rcsid_cookie_c = "$Id: cookie.c,v 1.0 1991/09/12 20:32:56 rosenkra Exp $";
  7. #endif
  8.  
  9. /*
  10.  * $Log: cookie.c,v $
  11.  * Revision 1.0  1991/09/12  20:32:56  rosenkra
  12.  * Initial revision
  13.  *
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <osbind.h>
  19. #include <stdio.h>
  20. #include "t100.h"
  21.  
  22. /* Most of the cookie code here by Eric R. Smith */
  23.  
  24.  
  25. /*
  26.  *    we use a union because most cookie tags are 4 ASCII characters,
  27.  *    so it may be useful to sometimes think of them as strings. On the
  28.  *    other hand, they *are* longwords, so we may want to access them
  29.  *    that way, too
  30.  */
  31. union clong
  32. {
  33.     char    aschar[4];
  34.     long    aslong;
  35. };
  36.  
  37.  
  38. /*
  39.  *    a cookie consists of 2 longwords; a tag and a value. The tag is
  40.  *    normally chosen to have some sort of significance when represented
  41.  *    as 4 ascii characters (see the union definition above). What the
  42.  *    value represents is dependent on the tag; it may be an address (for
  43.  *    a TSR), or a version number (e.g. MiNT does this), or whatever (it
  44.  *    may not even have a meaning).
  45.  */
  46. struct cookie
  47. {
  48.     union clong    tag;
  49.     long        value;
  50. };
  51.  
  52. typedef struct cookie   COOKIE;
  53.  
  54.  
  55. /*
  56.  *    A pointer to the cookie jar is found at 0x5a0. If there is no cookie
  57.  *    jar installed, this pointer will be 0. The cookie jar itself is an
  58.  *    array of cookies, with the last cookie having a "tag" of 0x00000000.
  59.  *    (The value of this cookie is the number of slots left in the cookie
  60.  *    jar.)
  61.  */
  62. #define CJAR        ((COOKIE **) 0x5a0)
  63.  
  64.  
  65.  
  66.  
  67. /*------------------------------*/
  68. /*    getcookie        */
  69. /*------------------------------*/
  70. long getcookie (char *cookieid)
  71. {
  72.     char    biscuit[5];
  73.     long    retval = 0L;        /* failure to find cookie ret this */
  74.     long    ssp;
  75.     char   *p;
  76.     COOKIE *cookie;
  77.  
  78.  
  79.  
  80.     /*
  81.      *   access the cookie jar in supervisor mode!
  82.      */
  83.     ssp    = Super (0L);
  84.     cookie = *CJAR;
  85.  
  86.  
  87.     /*
  88.      *   the value at CJAR will be non-null ptr if there is a jar.
  89.      */
  90.     if (cookie)
  91.     {
  92.         /*
  93.          *   traverse the list...
  94.          */
  95.         while (cookie->tag.aslong != 0)
  96.         {
  97.             /*
  98.              *   poke the cookie tag into something we can touch
  99.              */
  100.             p = cookie->tag.aschar;
  101.             sprintf (biscuit, "%c%c%c%c%c%c",
  102. #ifdef __GNUC__
  103.                 /* these should be int under ANSI C! */
  104.                 (int)(p[0]), (int)(p[1]), (int)(p[2]), (int)(p[3]), (int)('\000'));
  105. #else
  106.                 p[0], p[1], p[2], p[3], '\000');
  107. #endif
  108.  
  109.  
  110.             /*
  111.              *   is this the cookie we seek?
  112.              */
  113.             if (strncmp (cookieid, biscuit, 4) == 0)
  114.             {
  115.                 /*
  116.                  *   yes! quit looking...
  117.                  */
  118.                 retval = (cookie->value);
  119.                 break;
  120.             }
  121.  
  122.             /*
  123.              *   next cookie...
  124.              */
  125.             cookie++;
  126.         }
  127.     }
  128.  
  129.  
  130.     /*
  131.      *   reset to user mode before we bug out!
  132.      */
  133.     ssp = Super (ssp);
  134.  
  135.  
  136.     /*
  137.      *   return value of the cookie
  138.      */
  139.     return (retval);
  140. }
  141.